﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using nyxwin.Common.Base.Common;
using nyxwin.Common.Providers;
using System.Windows.Threading;
using nyxwin.Main.Base.Interfaces;
using Microsoft.Phone.Controls;

namespace nyxwin.Main.Base.ViewModels
{
    public class BaseViewModel : NotifyingObject
    {
        private System.ComponentModel.PropertyChangedEventHandler localizationChangedHandler;

        public BaseViewModel()
        {
            this.localizationChangedHandler = new System.ComponentModel.PropertyChangedEventHandler(Instance_PropertyChanged);
            LocalizationProvider.Instance.PropertyChanged += localizationChangedHandler;
        }

        void Instance_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            OnLocalizationChanged();
        }

        public LocalizationProvider Localization { get { return LocalizationProvider.Instance; } }
        public Dispatcher Dispatcher { get { return Deployment.Current.Dispatcher; } }

        public virtual void Unload()
        {
            LocalizationProvider.Instance.PropertyChanged -= localizationChangedHandler;
        }

        protected virtual void OnLocalizationChanged()
        { }

        #region Handling IsInDeterminate
        protected void SetIsInDeterminate(Guid id, string text)
        {
            var rootVisual = Application.Current.RootVisual as ContentControl;

            if (rootVisual != null)
            {
                var content = rootVisual.Content as ISupportProgress;

                content.SetInDeterminate(id, text);
            }
        }

        protected void UnSetInDeterminate(Guid id)
        {
            var rootVisual = Application.Current.RootVisual as ContentControl;

            if (rootVisual != null)
            {
                var content = rootVisual.Content as ISupportProgress;

                content.UnSetInDeterminate(id);
            }
        } 
        #endregion

        #region Handling Errors
        protected void RaiseError(string message)
        {
            var rootVisual = Application.Current.RootVisual as ContentControl;

            if (rootVisual != null)
            {
                var content = rootVisual.Content as ISupportErrors;

                if(content != null)
                    content.RaiseError(message);
            }
        }
        #endregion

        #region Navigation
        protected void Navigate(Uri targetUri)
        {
            var rootVisual = Application.Current.RootVisual as PhoneApplicationFrame;

            if (rootVisual != null)
            {
                rootVisual.Navigate(targetUri);
            }
        }

        protected void GoBack()
        {
            var rootVisual = Application.Current.RootVisual as PhoneApplicationFrame;

            if (rootVisual != null && rootVisual.CanGoBack)
            {
                rootVisual.GoBack();
            }
        } 
        #endregion
    }
}
